package cn.edu.hstc.wm.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.widget.EditText;
import android.widget.ImageButton;
import cn.edu.hstc.wm.Constants;
import cn.edu.hstc.wm.R;
import cn.edu.hstc.wm.ui.fragment.StoreMode;
import cn.edu.hstc.wm.ui.fragment.StoreItemMenu;

/**
 * @ClassName: SearchActivity
 * @Description: 搜索页面,根据不同来源的信息动态加载不同页面:good,stroe
 * 
 * @ModificationHistory
 */
public class SearchActivity extends FragmentActivity implements OnClickListener {

	private static final String TAG = SearchActivity.class.getName();

	private int mCurSearchType = 0;

	/** 返回按钮 */
	private ImageButton mBackButton;
	/** 搜索按钮 */
	private ImageButton mSearchButton;
	/** 输入框 */
	private EditText mInpuText;

	/** 搜索内容页面 */
	private Fragment mFragment;

	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		setContentView(R.layout.search);
		// 取得来源页面的信息
		mCurSearchType = getIntent().getIntExtra(Constants.SEARCH_TYPE, 0);
		Log.d(TAG, mCurSearchType + "");

		initActionbar();

		// initContentFragment();
	}

	/**
	 * 初始化Actionbar
	 */
	private void initActionbar() {
		mBackButton = (ImageButton) findViewById(R.id.ib_search_actionbar_back);
		mSearchButton = (ImageButton) findViewById(R.id.ib_search_actionbar_search);
		mInpuText = (EditText) findViewById(R.id.et_search_actionbar_input);

		mBackButton.setOnClickListener(this);
		mSearchButton.setOnClickListener(this);
		mInpuText.setOnKeyListener(new OnKeyListener() {
			public boolean onKey(View v, int keyCode, KeyEvent event) {
				if (keyCode == KeyEvent.KEYCODE_SEARCH) {
					mSearchButton.performClick();
					Log.d(TAG, "onKey " + keyCode + "   " + KeyEvent.KEYCODE_SEARCH);
				}
				return true;
			}
		});
	}

	/**
	 * 根据不同来源的信息,使用不同的内容页
	 */
	private void initContentFragment() {
		switch (mCurSearchType) {
		case Constants.SEARCH_TYPE_STORES:
			mFragment = new StoreMode();
			break;

		case Constants.SEARCH_TYPE_GOODS:
			mFragment = new StoreItemMenu();
			break;

		default:
			break;
		}

		FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
		transaction.add(R.id.fl_search_content, mFragment);
		transaction.commit();

	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
	}

	@Override
	protected void onResumeFragments() {
		// TODO Auto-generated method stub
		super.onResumeFragments();
	}

	@Override
	protected void onSaveInstanceState(Bundle outState) {
		// TODO Auto-generated method stub
		super.onSaveInstanceState(outState);
	}

	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
	}

	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
	}

	@Override
	public void startActivityForResult(Intent intent, int requestCode) {
		// TODO Auto-generated method stub
		super.startActivityForResult(intent, requestCode);
	}

	@Override
	public void startActivityFromFragment(Fragment fragment, Intent intent, int requestCode) {
		// TODO Auto-generated method stub
		super.startActivityFromFragment(fragment, intent, requestCode);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.ib_search_actionbar_back:
			finish();
			break;

		case R.id.ib_search_actionbar_search:
			search();
			break;

		default:
			break;
		}

	}

	private void search() {
		if (mFragment == null) {
			initContentFragment();
		}

	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (KeyEvent.KEYCODE_BACK == keyCode) {
			finish();
		}

		return false;
	}

	@Override
	public void onBackPressed() {
		super.onBackPressed();
	}

}
